package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusCustomer;
import com.example.cheeseerpadmin.bus.service.BusCustomerService;
import com.example.cheeseerpadmin.bus.vo.CustomerVo;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-14
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    public BusCustomerService busCustomerService;

    /**
     * 加载全部数据并且做模糊查询
     *
     * @param customerVo
     * @return
     */
    @RequestMapping("/loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo) {
        Page<BusCustomer> busCustomerPage = new Page<>();
        QueryWrapper<BusCustomer> busCustomerQueryWrapper = new QueryWrapper<>();
        busCustomerQueryWrapper.like(StringUtils.isNotBlank(customerVo.getCustomername()), "customername", customerVo.getCustomername());
        busCustomerQueryWrapper.like(StringUtils.isNotBlank(customerVo.getTelephone()), "telephone", customerVo.getTelephone());
        busCustomerQueryWrapper.like(StringUtils.isNotBlank(customerVo.getConnectionperson()), "connectionperson", customerVo.getConnectionperson());
        Page<BusCustomer> page = busCustomerService.page(busCustomerPage, busCustomerQueryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 增加客户
     *
     * @param busCustomer
     * @return
     */
    @RequestMapping("/addCustomer")
    public ResultObj addCustomer(BusCustomer busCustomer) {
        try {
            busCustomerService.save(busCustomer);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新客户
     *
     * @param customerVo
     * @return
     */
    @RequestMapping("/updateCustomer")
    public ResultObj updateCustomer(CustomerVo customerVo) {
        try {
            busCustomerService.updateById(customerVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 批量删除客户
     * @param customerVo
     * @return
     */
    @RequestMapping("/batchDeleteCustomer")
    public ResultObj batchDeleteCustomer(CustomerVo customerVo) {
        try {
            Integer[] ids = customerVo.getIds();
            for (Integer id : ids) {
                busCustomerService.removeById(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @RequestMapping("/deleteCustomerById")
    public ResultObj deleteCustomerById(Integer id){
        try {
            busCustomerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

